<?php

class model_menu extends model
{

	// 获取菜单组列表
	public function get_group_list()
	{
		$db = bone::get_db();
		return $db->get_objects( 'SELECT * FROM `bone_menu_group` ORDER BY `is_backend` ASC' );
	}

	// 获取菜单组中总数
	public function get_group_sum()
	{
		$db = bone::get_db();
		return $db->get_result( 'SELECT COUNT(*) FROM `bone_menu_group`' );
	}

	//删除菜单组
	public function delete_group( $group_id )
	{
		$db = bone::get_db();
		$db->execute( 'DELETE FROM `bone_menu` WHERE `group_id`='.$group_id );

		$group = bone::get_table('menu_group');
		$group->load( $group_id );

		if($group->is_backend)
			unlink( BONE_ROOT.DS.APP_DIR.DS.'menus'.DS.$group->class_name.'.php');
		else
			unlink( BONE_ROOT.DS.'menus'.DS.$group->class_name.'.php' );

		$group->delete();
	}


	// 获取菜单项列表
	public function get_menu_list( $group_id )
	{
		$db = bone::get_db();
		return $db->get_objects( 'SELECT * FROM `bone_menu` WHERE `group_id`='.$group_id.' ORDER BY `ordering` ASC' );
	}


	// 获取一个菜单下拥有的菜单项总数
	public function get_menu_sum( $group_id )
	{
		$db = bone::get_db();
		return $db->get_result( 'SELECT COUNT(*) FROM `bone_menu` WHERE `group_id`='.$group_id );
	}


	// 删除菜单
	public function delete_menu($menu_id)
	{
		$db = bone::get_db();
		$ids = $this->get_sub_menu_ids( $menu_id );
		$ids = implode(',',$ids);
		$db->execute( 'DELETE FROM `bone_menu` WHERE `id` IN('.$ids.')' );
		$db->execute( 'DELETE FROM `bone_menu` WHERE `id`='.$menu_id );

		return true;
	}


	// 保存菜单为静态 php 文件以提高执行效率
	public function update_menu_file($group_id)
	{
		$group = bone::get_table('menu_group');
		$group->load($group_id);

		$menu_list = $this->get_menu_list( $group_id );

		$s  = '<?php' . "\r\n";
		$s .= 'class menu_'.$group->class_name.' extends menu' . "\r\n";
		$s .= '{' . "\r\n";
		$s .= '  public function __construct()' . "\r\n";
		$s .= '  {' . "\r\n";
		foreach($menu_list as $menu)
		{
			$s .= '    $this->add_menu('.$menu->id.', '.$menu->parent_id.', \''.$menu->name.'\', \''.$menu->controller.'\', \''.$menu->task.'\', \''.$menu->param.'\', \''.$menu->url.'\');'."\r\n";
		}
		$s .= '  }' . "\r\n";
		$s .= '}' . "\r\n";
		$s .= '?>';

		if($group->is_backend)
			file_put_contents( BONE_ROOT.DS.APP_DIR.DS.'menus'.DS.$group->class_name.'.php', $s );
		else
			file_put_contents( BONE_ROOT.DS.'menus'.DS.$group->class_name.'.php', $s );
	}


	// 获取文章分类树
	public function get_menu_tree($group_id)
	{
		$menus = $this->get_menu_list($group_id);
		return $this->create_menu_tree( $menus );
	}
	// 创建菜单树
	private function create_menu_tree($menus, $menu_id=0, $level=0)
	{
		$tree = array();
		foreach($menus as $menu)
		{
			if($menu->parent_id==$menu_id)
			{
				$menu->level = $level;
				$menu->sub_menu = $this->create_menu_tree($menus, $menu->id, $level+1);
				$menu->children = count($menu->sub_menu);
				$tree[] = $menu;
			}
		}
		if(count($tree))
			return $tree;
		return null;
	}


	// 获取文章分类树线性列表
	public function get_menu_tree_list($group_id)
	{
		$tree = $this->get_menu_tree($group_id);
		return $this->create_menu_tree_list($tree);
	}
	// 创建菜单树线性列表
	private function create_menu_tree_list(&$tree, &$list = array())
	{
		if(count($tree))
		{
			foreach($tree as $menu)
			{
				$list[] = $menu;
				if($menu->sub_menu) $this->create_menu_tree_list($menu->sub_menu, $list);
			}
		}
		return $list;
	}

	/*
	public function get_menu_tree_list($group_id)
	{
		$menus = $this->get_menu_list($group_id);
		return $this->create_menu_tree_list($menus);
	}
	private function create_menu_tree_list(&$menus, &$list = array(), $parent_id=0, $level=0)
	{

		foreach($menu_list as $menu)
		{
			if($menu->parent_id == $parent_id)
			{
				$menu->level = $level;
				$list[] = $menu;

				$this->create_menu_tree_list($menus, $list, $menu->id, $level+1);
			}
		}
		return $list;
	}
	*/



	private function get_sub_menu_ids($menu_id, &$ids = array() )
	{
		$db = bone::get_db();
		$objects = $db->get_objects( 'SELECT `id` FROM `bone_menu` WHERE `parent_id`='.$menu_id );
		foreach($objects as $object)
		{
			$ids[] = $object->id;
			$this->get_sub_menu_ids($object->id, $ids);
		}

		return $ids;
	}


}
?>